<template>
  <div class="yy-gd-repair-management">
    <!--列表-->
    <div class="yy-goods-manage-list" v-show="type=='list'">
      <Card>
        <p slot="title">
          <Icon type="md-list" />
          工单报修与管理
        </p>
        <Form ref="formInline" :model="formSearch"  inline>
            <FormItem prop="user">
              <Input type="text" v-model="formSearch.gdCode" placeholder="请输入工单号"></Input>
            </FormItem>
            <FormItem prop="cateName">
              <Select class="select" v-model="formSearch.cateName"  placeholder="请选择工单状态">
                <Option value="">全部</Option>
                <Option value="1">待派发</Option>
                <Option value="2">待受理</Option>
                <Option value="3">执行中</Option>
                <Option value="4">待关单</Option>
                <Option value="5">已完成</Option>
                <Option value="6">已取消</Option>
              </Select>
            </FormItem>
            <FormItem prop="user">
              <DatePicker type="date" placeholder="起始时间" style="width: 200px" v-model="formSearch.st" :options="startTimeOption" @on-change="onStartTimeChange"></DatePicker>
            </FormItem>
            <FormItem prop="user">
              <DatePicker type="date" placeholder="结束时间" style="width: 200px" v-model="formSearch.et" :options="endTimeOption" @on-change="onEndTimeChange"></DatePicker>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleSearch()">查询</Button>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleReset()">重置</Button>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleAdd()">报修</Button>
            </FormItem>
        </Form>
        <Table :columns="columns" :data="data" ></Table>
        <div style="margin: 10px;overflow: hidden">
          <div style="float: right;">
              <Page :total="page.total" :page-size="page.pageSize" :current="1" @on-change="changePage" show-total></Page>
          </div>
        </div>
      </Card>
    </div>
    <!--详情-->
    <div class="yy-goods-manage-details" v-show="type=='details'">
       <Card>
        <p slot="title">
          <Icon type="md-paper"/>
          查看详情
        </p>
        <p slot="extra">
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="details">
          <p class="details-title"><span>物品编号:</span>{{currentData.itemNum}}</p>
          <p class="details-title"><span>物品名称:</span>{{currentData.itemName}}</p>
          <p class="details-title"><span>预警下限:</span>{{currentData.minNum}}</p>
          <p class="details-title"><span>预警上限:</span>{{currentData.maxNum}}</p>
          <p class="details-title"><span>单位:</span>{{currentData.unitName}}</p>
          <p class="details-title"><span>是否耐用:</span>{{currentData.isSpare}}</p>
          <p class="details-title"><span>是否返修件:</span>{{currentData.isFX}}</p>
          <p class="details-title"><span>分类名称:</span>{{currentData.cateName}}</p>
          <p class="details-title"><span>规格:</span>{{currentData.size}}</p>
          <p class="details-title"><span>主要供货商:</span>{{currentData.cusName}}</p>
          <p class="details-title"><span>备注:</span>{{currentData.remark}}</p>
        </div>
      </Card>
    </div>
    <!--添加/编辑-->
    <div class="yy-goods-manage-add-edit"  v-show="(type=='add' || type=='edit')">
      <Card>
        <p slot="title">
          <template v-if="type=='add'"> 
            <Icon type="md-create"/> 新增
          </template>
          <template v-if="type=='edit'"> 
            <Icon type="md-document"/>编辑
          </template>
        </p>
        <p slot="extra" >
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="editForm">
          <Form ref="editForm" :model="editFormInLine" :rules="editFormRuleInline" :label-width="100" @keydown.native.enter.prevent ="handleSubmit">
            <FormItem label="物品编号"  prop="itemNum">
              <Input v-model="editFormInLine.itemNum" placeholder="请输入物品编号"  :disabled="type=='edit'? true:false"></Input>
            </FormItem>
            <FormItem label="物品名称" prop="itemName">
              <Input v-model="editFormInLine.itemName" placeholder="请输入物品名称" :disabled="type=='edit'? true:false"></Input>
            </FormItem>   
            <FormItem label="分类名称" prop="cateNum" placeholder="请选择分类名称" >
            <Select class="select" v-model="editFormInLine.cateNum"  placeholder="请选择分类名称" :disabled="type=='edit'? true:false">
              <Option v-for="(dataSt,index) in dataCateName" :value="dataSt.cateNum" :key="index" :label="dataSt.cateName">
                {{dataSt.cateName}}
              </Option>
            </Select>
            </FormItem>      
            <FormItem label="预警下限" prop="minNum">
              <InputNumber v-model="editFormInLine.minNum" min=0 placeholder="预警下限"></InputNumber>
            </FormItem>
            <FormItem label="预警上限" prop="maxNum">
              <InputNumber v-model="editFormInLine.maxNum" min=0 placeholder="预警上限"></InputNumber>
            </FormItem>
            <FormItem label="单位" prop="unitName">
              <Input v-model="editFormInLine.unitName" placeholder="单位"></Input>
            </FormItem>
            <FormItem label="是否耐用" prop="isSpare">
            <Select class="select" v-model="editFormInLine.isSpare" placeholder="是否耐用">
              <Option :value=0>否</Option>
              <Option :value=1>是</Option>
            </Select>
            </FormItem>
            <FormItem label="是否返修件" prop="isFX">
            <Select class="select" v-model="editFormInLine.isFX" placeholder="是否返修件">
              <Option :value=0>否</Option>
              <Option :value=1>是</Option>
            </Select>
            </FormItem>
            <FormItem label="规格" prop="size">
              <Input v-model="editFormInLine.size" placeholder="规格"></Input>
            </FormItem>
            <FormItem label="主要供货商" prop="cusName">
              <Input v-model="editFormInLine.cusName" placeholder="主要供货商"></Input>
            </FormItem>
            <FormItem label="备注" prop="remark">
              <Input v-model="editFormInLine.remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="备注" maxlength="50"></Input>
            </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" class="button">提交</Button>
              <Button @click="handleReset" type="primary" class="button">重置</Button>
            </FormItem>
          </Form>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import API from "./api";
import ValidatorRule from "@/filters/validator-rule";
import { formatNetTime } from "@/libs/tools";
import i18n from "./locale";

export default {
  name: "yy-gd-repair-management",
  data() {
    return {
      i18n: i18n,
      currentData: "",
      detailsModal: false,
      type: "list",
      dataCateName: [],
      page: {
        total: 0,
        pageSize: 20
      },
      formSearch: {
        itemNum: "",
        itemName: "",
        cateName: "",
        userID: "",
        userName: "",
        b_UserRole: ""
      },
      editFormInLine: {
        cateNum: "",
        cateName: "",
        itemNum: "",
        itemName: "",
        itemBarCode: "",
        minNum: "",
        maxNum: "",
        unitName: "",
        isSpare: 0,
        isFX: 0,
        size: "",
        color: "",
        cusName: "",
        description: "",
        remark: "",
        userID: "",
        userName: "",
        b_UserRole: "",
        brand: ""
      },
      editFormRuleInline: {
        itemNum: ValidatorRule.require,
        itemName: ValidatorRule.require,
        cateNum: ValidatorRule.select
      },
      columns: [
        {
          title: "工单号",
          key: "gdCode"
        },
        {
          title: "工单状态",
          key: "gdZht"
        },
        {
          title: "故障位置 ",
          key: "wxDidian"
        },
        {
          title: "维修事项 ",
          key: "wxXm"
        },
        {
          title: "故障描述 ",
          key: "bxBeizhu"
        },
        {
          title: "报修时间",
          key: "gdShij"
        },
        {
          title: "接单时间",
          key: "paiGongShij"
        },
        {
          title: "完工申请时间",
          key: "shouLiShij"
        },
        {
          title: "关单时间",
          key: "gdFsShij"
        },
        {
          title: "维修科室",
          key: "bxKeshi"
        },
        {
          title: "执行人",
          key: "wxUserNames"
        },
        {
          title: "联系电话",
          key: "bxDianhua"
        },
        {
          title: "验收人",
          key: "remark"
        },
        {
          title: "操作",
          key: "action",
          width: 200,
          align: "center",
          className: "print-no-show",
          render: (h, params) => {
            let cancelAction = h(
              "Button",
              {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.cancel(params.index);
                  }
                }
              },
              "取消工单"
            );

            let changeAction = h(
              "Button",
              {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.change(params.index);
                  }
                }
              },
              "确认费用"
            );

            let distributedAction = h(
              "Button",
              {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.distributed(params.index);
                  }
                }
              },
              "添加备注"
            );

            //根据角色判断操作按钮的显示
            let arr = [cancelAction, changeAction, distributedAction];
            return h("div", arr);
          }
        }
      ],
      data: []
    };
  },

  mounted() {
    this.userInfo = this.$store.state.user.userInfo;
    this.handleSearch();
  },

  computed: {},

  methods: {
    handleSearch(index, id) {
      let obj = this.formSearch;
      obj.pageNum = 1;
      obj.pageSize = this.page.pageSize;
      this.formSearch = obj;
      this.getList(obj);
    },

    changePage(pageNum) {
      let obj = this.formSearch,
        maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.formSearch = obj;
      this.getList(obj);
    },

    getList(obj) {
      API.getList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            if (data != null) {
              this.page.total = data.total;
              this.page.pageSize = data.pageSize;
              this.data = data.list;
            } else {
              this.$Message.success({ content: "暂无数据！" });
              this.data = [];
            }
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    goBack() {
      this.type = "list";
    },

    handleAdd() {
      this.type = "add";
      this.handleReset();
    },

    details(index) {
      this.type = "details";
      this.currentData = _.clone(this.data[index]);
      if (this.currentData.isSpare == 0) {
        this.currentData.isSpare = "否";
      } else {
        this.currentData.isSpare = "是";
      }
      if (this.currentData.isFX == 0) {
        this.currentData.isFX = "否";
      } else {
        this.currentData.isFX = "是";
      }
    },

    edit(index) {
      this.type = "edit";
      this.handleReset();
      this.currentData = this.data[index];
      //this.editFormInLine.cateNum = this.currentData.cateNum;
      this.editFormInLine = _.clone(this.currentData);
    },

    delete(index, id) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除这条记录",
        onOk: function() {
          let obj = {
            ItemNum: tthis.data[index].itemNum,
            ItemName: tthis.data[index].itemName,
            userID: tthis.userInfo.userId,
            userName: tthis.userInfo.userName,
            b_UserRole: ""
          };
          API.delete(obj)
            .then(res => {
              if (res.code == 200) {
                this.$Message.success({ content: "该记录已经删除成功！" });
                tthis.data.splice(index, 1);
                tthis.resetList();
              }
            })
            .catch(err => {
              console.log(err);
              this.$Message.error("请求异常！");
            });
        }
      });
    },

    resetList() {
      //求余为1，页码为最大页码，请求页需减1
      let maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      let remainderNum = this.page.total % this.page.pageSize;
      if (
        this.formSearch.pageNum == maxPageNum &&
        remainderNum == 1 &&
        this.formSearch.pageNum !== 1
      ) {
        this.formSearch.pageNum = this.formSearch.pageNum - 1;
      }
      let obj = this.formSearch;
      this.getList(obj);
    },

    handleSubmit() {
      //console.log(384, this.editFormInLine);
      this.$refs.editForm.validate(valid => {
        if (valid) {
          if (this.type == "edit") {
            this.editSubmit(this.editFormInLine);
          } else {
            this.addSubmit(this.editFormInLine);
          }
        }
      });
    },

    handleReset() {
      this.$refs.editForm.resetFields();
      if (this.type == "edit") {
        this.editFormInLine.itemNum = this.currentData.itemNum;
        this.editFormInLine.itemName = this.currentData.itemName;
        this.editFormInLine.cateNum = this.currentData.cateNum;
      }
    },

    editSubmit(obj) {
      this.formSearch.cateNum = obj.cateNum;
      obj.userID = this.userInfo.userId;
      //obj.id = this.currentData.id;
      API.editSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.resetList();
            this.$Message.success("修改成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("修改异常！");
        });
    },

    addSubmit(obj) {
      obj.userID = this.userInfo.userId;
      API.addSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.handleSearch();
            this.$Message.success("添加成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    }
  }
};
</script>
<style lang="less" >
@import "./yy-gd-repair-management.less";
</style>
